#include <atomic>
#include <iostream>
#include <thread>

struct X {
    int i;
    std::string s;
};

std::atomic<X*> p;
std::atomic<int> a;

void create_X() {
    X* x = new X;
    x->i = 42;
    x->s = "hello";
    // 依赖关系
    a.store(99, std::memory_order_relaxed);
    p.store(x, std::memory_order_release);
}

void use_x() {
    X* x;
    while (!(x = p.load(std::memory_order_consume))) {
        std::this_thread::sleep_for(std::chrono::microseconds(1));
    }
    if (x->i == 42) {
        std::cout << "x->i == 42 is true\n";
    }
    if (x->s == "hello") {
        std::cout << "x->s == hello is true\n";
    }
    if (a.load(std::memory_order_relaxed) == 99) {
        std::cout << "a.load(std::memory_order_relaxed) == 99\n";
    }
}

int main(int argc, char const *argv[])
{
    std::thread t1(create_X);
    std::thread t2(use_x);

    t1.join();
    t2.join();
    
    return 0;
}
